﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Threading;
using LogLib;

namespace ComUIControl
{
    public partial class SubtitleControl : UserControl, INotifyPropertyChanged
    {
        public SubtitleControl()
        {
            this.Visible = false;
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
           // SetStyle(ControlStyles.Opaque, true);
           // this.BackColor = Color.Transparent;          
            InitializeComponent();
            labelTitle.DataBindings.Add("Text", this, "SubTitle");
            InitTimer();           
        }

        private string _subTitle;
        [DefaultValue("提示")]
        public string SubTitle
        {
            get
            {
                return _subTitle;
            }
            set
            {
                if (_subTitle != value)
                {
                    _subTitle = value;
                    if (!this.InvokeRequired)
                    {
                        labelTitle.Text = value;
                    }
                    this.RaisePropertyChanged("SubTitle");
                }
            }
        }

        /// <summary>
        /// 在父窗口的位置
        /// </summary>
        private DockPosition _dockPos;
        [DefaultValue(DockPosition.Bottom)]
        public DockPosition DockPos
        {
            get
            {
                return _dockPos;
            }
            set
            {
                _dockPos = value;
            }
        }

        private uint _showTime = 3;
        [DefaultValue(3)]
        public uint ShowTime
        {
            get
            {
                return _showTime;
            }
            set
            {
                _showTime = value;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public delegate void UpdatePropertyDelegate(string strName);
        private void RaisePropertyChanged(string propertyName)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    UpdatePropertyDelegate outdelegate = new UpdatePropertyDelegate(RaisePropertyChanged);
                    this.BeginInvoke(outdelegate, new object[] { propertyName });
                    return;
                }
                var handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            catch (System.Exception e)
            {
                Log.WriteAsync("捕获异常:" + e.Message);
            }
        }

        private System.Timers.Timer timer;
        private SynchronizationContext context;

        private void InitTimer()
        {
            context = SynchronizationContext.Current;
            timer = new System.Timers.Timer();
            timer.Interval = ShowTime * 1000;
            timer.AutoReset = false;
            timer.Elapsed += Timer_Elapsed;
        }

        private void Timer_Elapsed(object o, ElapsedEventArgs e)
        {
            //context.Post(delegate
            //{
            //    if (this.Visible)
            //    {
            //        this.Visible = false;     
            //    }
            //    // SubTitle = "null";
            //    timer.Stop();
            //}, null);
        }

        public delegate void ShowTitleDelegate(IWin32Window owner, string title, DockPosition dockPos);
        public void Show(IWin32Window owner, string title, DockPosition dockPos)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    ShowTitleDelegate outdelegate = new ShowTitleDelegate(Show);
                    this.BeginInvoke(outdelegate, new object[] { owner, title, dockPos });
                    return;
                }
                //p = PointToScreen(p);
                timer.Stop();              
                this.Visible = true;
                SubTitle = title;
                ResetLocation(owner, dockPos);
                timer.Start();
            }
            catch (System.Exception e)
            {
                Log.WriteAsync("捕获异常:" + e.Message);
            }
        }

        public void HideWnd()
        {
            try
            {
                if (this.InvokeRequired)
                {
                    Action outdelegate = new Action(Hide);
                    this.BeginInvoke(outdelegate, new object[] {  });
                    return;
                }
                //p = PointToScreen(p);
                timer.Stop();
                this.Visible = false;
                SubTitle = "";               
            }
            catch (System.Exception e)
            {
                Log.WriteAsync("捕获异常:" + e.Message);
            }
        }

        private void ResetLocation(IWin32Window owner, DockPosition p = DockPosition.Bottom)
        {
            Form _owner = (owner as Form == null) ? ((UserControl)owner).ParentForm : (Form)owner;
            Size titleSize = labelTitle.Size;
            int height = titleSize.Height + 30;
            int width = titleSize.Width < _owner.Width ? titleSize.Width + 20 : titleSize.Width;
            this.Size = new Size(width, height);
            int x = /*_owner.Location.X +*/ (_owner.Width - this.Width) / 2;
            int y = 0;
            switch (p)
            {
                case DockPosition.Top:
                    y = 420;
                    break;
                case DockPosition.middle:
                    y = (_owner.Height - this.Height)/2 - 20;
                    break;
                case DockPosition.Bottom:
                    y = _owner.Height - this.Height - 100;
                    break;
                default:
                    break;
            }
            /*_owner.Location.Y +*/
            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;

            this.Location = new Point(x, y);
            SetBackColor(Color.FromArgb(15, 0, 100, 200));
        }

        public void SetBackColor(Color col)
        {
            this.BackColor = col;
            this.labelTitle.BackColor = Color.Transparent;
        }
    };

    public enum DockPosition : uint
    {
        Top = 0, // 窗口顶部
        middle,  // 窗口中间
        Bottom  // 窗口底部
    }
}
